//
//  CZQScanCoverView.m
//  LoveBeer
//
//  Created by 崔正强 on 17/5/7.
//  Copyright © 2017年 崔正强. All rights reserved.
//

#import "CZQScanCoverView.h"
#import "LoveBeer.pch"
#define lineLenghth 20.0

@interface CZQScanCoverView()

@property (strong, nonatomic) NSTimer *timer;

@end

@implementation CZQScanCoverView

- (void)drawRect:(CGRect)rect{
    
    
    //中间镂空的矩形框
    CGRect myRect = CGRectMake(100,200, 200, 200);
    //背景
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:0];
    //镂空
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithRect:myRect];
    [path appendPath:circlePath];
    [path setUsesEvenOddFillRule:YES];
    
    CAShapeLayer *fillLayer = [CAShapeLayer layer];
    fillLayer.path = path.CGPath;
    fillLayer.fillRule = kCAFillRuleEvenOdd;//中间镂空的关键点 填充规则
    fillLayer.fillColor = [UIColor grayColor].CGColor;
    fillLayer.opacity = 0.5;
    [self.layer addSublayer:fillLayer];
    
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    
    [bezierPath moveToPoint:CGPointMake(100, 200+lineLenghth)];
    [bezierPath addLineToPoint:CGPointMake(100, 200)];
    [bezierPath addLineToPoint:CGPointMake(100+lineLenghth, 200)];
    
    [bezierPath moveToPoint:CGPointMake(300 - lineLenghth, 200)];
    [bezierPath addLineToPoint:CGPointMake(300, 200)];
    [bezierPath addLineToPoint:CGPointMake(300,200+lineLenghth )];
    
    [bezierPath moveToPoint:CGPointMake(300, 400 - lineLenghth)];
    [bezierPath addLineToPoint:CGPointMake(300, 400)];
    [bezierPath  addLineToPoint:CGPointMake(300- lineLenghth, 400)];
    
    [bezierPath moveToPoint:CGPointMake(100+lineLenghth, 400)];
    [bezierPath addLineToPoint:CGPointMake(100, 400)];
    [bezierPath addLineToPoint:CGPointMake(100, 400 - lineLenghth)];
    
    bezierPath.lineWidth = 5.0;
    bezierPath.lineCapStyle =  kCGLineCapSquare;
    bezierPath.lineJoinStyle = kCGLineJoinMiter;
    
    [appColor setStroke];
    [bezierPath stroke];
    
}


- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]){
        [self setupUI];
        
    }
    return self;
}

- (void)setupUI{
    UILabel* label = [[UILabel alloc]initWithFrame:CGRectMake(90, 410, 300, 20)];
    label.text = @"将店铺二维码对准方块内即可收藏店铺";
    label.font = [UIFont systemFontOfSize:13];
    label.textColor = [UIColor darkGrayColor];
    
    [self addSubview:label];
    
}


- (void)startScan{
    UIView* lineImg = [[UIView alloc]initWithFrame:CGRectMake(100, 200, 200, 2)];
    lineImg.backgroundColor = appColor;
    [self addSubview:lineImg];
    
    [UIView animateWithDuration:1.8 animations:^{
        lineImg.transform = CGAffineTransformMakeTranslation(0, 200);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 animations:^{
            lineImg.alpha = 0.0;
        } completion:^(BOOL finished) {
            [lineImg removeFromSuperview];
        }];
    }];
    
}


- (void)startScanAnimation{
    NSTimer* timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(startScan) userInfo:nil repeats:YES];
    _timer = timer;
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

- (void)endScanAnimation{
    [self.timer invalidate];
}




@end
